package com.uchedao.vehicledetection.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.global.GlobalInfo;
import com.uchedao.vehicledetection.inf.IitemChickBack;
import com.uchedao.vehicledetection.model.Draft;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wushan
 *         可展开（收缩）列表示例
 */
public class IdeasExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext = null;
    // 测试数据，开发时可能来自数据库，网络....
    private final String[] groups = GlobalInfo.resources.getStringArray(R.array.menu_group);
    private int[] childredIds = {R.array.base_msg, R.array.complex_msg, R.array.accident_msg, R.array.water_msg, R.array.fire_msg, R.array.photo_msg};

    private List<String> groupList = null;
    private List<List<String>> itemList = null;
    private IitemChickBack back;
    private String selectedItem = "手续信息";
    private boolean isShowOk = false;
    private int drawableId[] = {R.mipmap.ideasexpandable_1,R.mipmap.ideasexpandable_2,R.mipmap.ideasexpandable_3,R.mipmap.ideasexpandable_4,R.mipmap.ideasexpandable_5,R.mipmap.ideasexpandable_6};

    public IdeasExpandableListAdapter(Context context, IitemChickBack back) {
        this.mContext = context;
        this.back = back;
        groupList = new ArrayList<String>();
        itemList = new ArrayList<List<String>>();
        initData();
    }

    public List<List<String>> getItemList() {
        return itemList;
    }

    public void setSelectedItem(String item) {
        selectedItem = item;
        notifyDataSetChanged();
    }

    /**
     * 是否需要显示红色感叹号，表示该item下必填项目没有填满
     */
    public void showIsOk(boolean isShowOk) {
        this.isShowOk = isShowOk;
        notifyDataSetChanged();

    }

    /**
     * 初始化数据，将相关数据放到List中，方便处理
     */
    private void initData() {

        groupList = Arrays.asList(groups);

        for (int i = 0; i < childredIds.length; i++) {
            String[] item = GlobalInfo.resources.getStringArray(childredIds[i]);
            itemList.add(Arrays.asList(item));
        }

    }

    public boolean areAllItemsEnabled() {
        return false;
    }

    /*
     * 设置子节点对象，在事件处理时返回的对象，可存放一些数据
     */
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /*
     * 字节点视图，这里我们显示一个文本对象
     */
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderChildren viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolderChildren();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_main_children_view, null);
            viewHolder.childOkTv = (TextView) convertView.findViewById(R.id.item_main_children_ok_tv);
            viewHolder.childTitle = (TextView) convertView.findViewById(R.id.item_main_children_title_tv);
            viewHolder.childImg = (ImageView) convertView.findViewById(R.id.item_main_children_right_img_iv);
            viewHolder.childRightTv = (TextView) convertView.findViewById(R.id.item_main_children_right_img_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolderChildren) convertView.getTag();
        }
        // 获取子节点要显示的名称
        final String name = (String) itemList.get(groupPosition).get(childPosition);
        viewHolder.childTitle.setText(name);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedItem = name;
                notifyDataSetChanged();
                back.execute(groupPosition, childPosition, name);
            }
        });

//        System.out.println("ws----selectedItem = "+selectedItem+"   name="+name);
        if (selectedItem.equals(name)) {
            viewHolder.childRightTv.setTextColor(mContext.getResources().getColor(R.color.app_style_blue));
            viewHolder.childTitle.setTextColor(mContext.getResources().getColor(R.color.app_style_blue));
        } else {
            viewHolder.childRightTv.setTextColor(mContext.getResources().getColor(R.color.item_child_icon_color));
            viewHolder.childTitle.setTextColor(mContext.getResources().getColor(R.color.item_child_icon_color));
        }

        if (isShowOk) {

            System.out.println("ws---" + groupPosition + " " + childPosition + "   apapter item--status-containsKey=" + GlobalInfo.draft.statusChildren.containsKey(groupPosition + "" + childPosition));
            System.out.println("ws---" + groupPosition + " " + childPosition + "   apapter item--status=" + GlobalInfo.draft.statusChildren.get(groupPosition + "" + childPosition));

            if (groupPosition == 0 || groupPosition == 1) {

                if (GlobalInfo.draft.statusChildren.containsKey(groupPosition + "" + childPosition) &&
                        GlobalInfo.draft.statusChildren.get(groupPosition + "" + childPosition)) {
                    viewHolder.childOkTv.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.childOkTv.setVisibility(View.GONE);
                }

            } else if (groupPosition == 5) {
                if (GlobalInfo.draft.photo[0] != null && !TextUtils.isEmpty(GlobalInfo.draft.photo[0].pic_path)) {
                    viewHolder.childOkTv.setVisibility(View.GONE);
                } else {
                    viewHolder.childOkTv.setVisibility(View.VISIBLE);
                }
            } else {
                viewHolder.childOkTv.setVisibility(View.GONE);
            }
        } else {
            viewHolder.childOkTv.setVisibility(View.GONE);
        }

        return convertView;
    }

    /*
     * 返回当前分组的字节点个数
     */
    public int getChildrenCount(int groupPosition) {
        return itemList.get(groupPosition).size();
    }

    /*
     * 返回分组对象，用于一些数据传递，在事件处理时可直接取得和分组相关的数据
     */
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    /*
     * 分组的个数
     */
    public int getGroupCount() {
        return groupList.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /*
     * 分组视图，这里也是一个文本视图
     */
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ViewHolderGroup viewHolder = null;
        TextView text = null;
        if (convertView == null) {
            viewHolder = new ViewHolderGroup();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_main_group_view, null);
            viewHolder.groupOkTv = (TextView) convertView.findViewById(R.id.item_main_group_ok_tv);
            viewHolder.groupTitle = (TextView) convertView.findViewById(R.id.item_main_group_title_tv);
            viewHolder.groupImg = (ImageView) convertView.findViewById(R.id.item_main_group_right_img_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolderGroup) convertView.getTag();
        }


        viewHolder.groupTitle.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(drawableId[groupPosition]),null, null, null);

        String name = (String) groupList.get(groupPosition);


        if (isExpanded) {
            viewHolder.groupImg.setBackgroundResource(R.mipmap.ic_item_up);
        } else {
            viewHolder.groupImg.setBackgroundResource(R.mipmap.ic_item_down);
        }


        viewHolder.groupTitle.setText(name);

        if (isShowOk) {
            if (groupPosition == 0 || groupPosition == 1) {
                if (GlobalInfo.draft.status[groupPosition] == Draft.STATUE_FINISH) {
                    viewHolder.groupOkTv.setVisibility(View.GONE);
                } else {
                    viewHolder.groupOkTv.setVisibility(View.VISIBLE);
                }
            } else if (groupPosition == 5) {
                if (GlobalInfo.draft.photo[0] != null && !TextUtils.isEmpty(GlobalInfo.draft.photo[0].pic_path)) {
                    viewHolder.groupOkTv.setVisibility(View.GONE);
                } else {
                    viewHolder.groupOkTv.setVisibility(View.VISIBLE);
                }
            } else {
                viewHolder.groupOkTv.setVisibility(View.GONE);
            }
        } else {
            viewHolder.groupOkTv.setVisibility(View.GONE);
        }

        return convertView;
    }

    /*
     * 判断分组是否为空，本示例中数据是固定的，所以不会为空，我们返回false 如果数据来自数据库，网络时，可以把判断逻辑写到这个方法中，如果为空
     * 时返回true
     */
    public boolean isEmpty() {
        return false;
    }

    /*
     * 收缩列表时要处理的东西都放这儿
     */
    public void onGroupCollapsed(int groupPosition) {

    }

    /*
     * 展开列表时要处理的东西都放这儿
     */
    public void onGroupExpanded(int groupPosition) {

    }

    /*
     * Indicates whether the child and group IDs are stable across changes to
     * the underlying data.
     */
    public boolean hasStableIds() {
        return false;
    }

    /*
     * Whether the child at the specified position is selectable.
     */
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class ViewHolderGroup {
        TextView groupOkTv;
        TextView groupTitle;
        ImageView groupImg;
    }

    public class ViewHolderChildren {
        TextView childOkTv;
        TextView childTitle;
        ImageView childImg;
        TextView childRightTv;
    }
}
